package com.third.clock.manager;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.orhanobut.logger.Logger;
import com.third.clock.R;
import com.third.clock.common.ConfigureLog4JUtil;
import com.third.clock.common.LogUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/7/21 0021.
 */

public class LocationManager implements IBaseManager {

    private Context mContext;

    private AMapLocation mAMapLocation;

    //声明mlocationClient对象
    public AMapLocationClient mlocationClient;

    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;

    private static final String NOTIFICATION_CHANNEL_NAME = "BackgroundLocation";

    private NotificationManager notificationManager = null;

    boolean isCreateChannel = false;

    public LocationManager(Context context) {
        this.mContext = context;
    }

    @Override
    public void onCreate() {
        initLocation();
    }

    @Override
    public void onDestroy() {

    }

    public void startLocation() {
        if (mlocationClient != null) mlocationClient.startLocation();
    }

    public void stopLocation() {
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
        }
    }

    private void initLocation() {
        mlocationClient = new AMapLocationClient(mContext);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mlocationClient.setLocationListener(mAMapLocationListener);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(1000*5);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
    }

    AMapLocationListener mAMapLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            Logger.d("onLocationChanged");
            if (aMapLocation != null) {
                ConfigureLog4JUtil.getInstall().printLogger("aMapLocation getErrorCode()"+aMapLocation.getErrorCode());
                if (aMapLocation.getErrorCode() == 0) {
                    mAMapLocation=aMapLocation;
                    //定位成功回调信息，设置相关消息
                    aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                    aMapLocation.getLatitude();//获取纬度
                    aMapLocation.getLongitude();//获取经度
                    aMapLocation.getAccuracy();//获取精度信息
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date(aMapLocation.getTime());
                    df.format(date);//定位时间
                    //上传GPS
                } else {
                    //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                    String AmapError="location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo();
                    LogUtils.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                    ConfigureLog4JUtil.getInstall().printLogger("AmapError"+AmapError);

                }
            }
        }
    };

  public AMapLocation getAMapLocation(){
      return mAMapLocation;
  }


    @SuppressLint("NewApi")
    private Notification buildNotification() {

        Notification.Builder builder = new Notification.Builder(mContext.getApplicationContext());

        Notification notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND;



        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("豆盯")
                .setContentText("正在后台运行")
                .setWhen(System.currentTimeMillis());

        if (android.os.Build.VERSION.SDK_INT >= 16) {
            notification = builder.build();
        } else {
            return builder.getNotification();
        }
        return notification;
    }

    public void enableBackgroundLocation(){
       //启动后台定位，第一个参数为通知栏ID，建议整个APP使用一个
        mlocationClient.enableBackgroundLocation(2001, buildNotification());
    }

    public void disableBackgroundLocation(){
        //启动后台定位，第一个参数为通知栏ID，建议整个APP使用一个
        mlocationClient.disableBackgroundLocation(true);
    }

}
